package com.example.shoppingcenter.Mapper;

import com.example.shoppingcenter.POJO.Chat;
import com.example.shoppingcenter.POJO.Messages;
import com.example.shoppingcenter.POJO.Transcript;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface xfMapper {
    @Select("Select * from chat where user_id = #{id}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "lastMessagesId", column = "last_messages_id"),
            @Result(property = "chatName", column = "chat_name"),
    })
    List<Chat> getChatByUserId(int id);

    @Insert("Insert into messages (cid,parent_id,user_content,model_content) values (#{chatId},#{lastMessageId},#{userContent},#{modelContent})")
    void saveMessageByChatId(Integer chatId, Integer lastMessageId, String userContent, String modelContent);

    @Insert("Insert into chat (user_id,chat_name) values (#{userId},CONCAT('chatByUser', #{userId}));")
    void createChatInUserId(Integer userId);
    @Update("UPDATE chat SET chat_name = CONCAT('newChat', #{userId}) where id = #{chatId}")
    void updateChatName(Integer chatId);
    @Select("SELECT id FROM chat WHERE user_id = #{userId} ORDER BY time DESC LIMIT 1")
    Integer getLastCreateChat(Integer userId);

//    @Select("Select id from chat where user_id= #{userId} and chat_name = CONCAT('chatByUser', #{userId});")
//    Integer getChatByChatName(Integer userId);

    @Select("SELECT *FROM messages WHERE cid = #{chatID} ORDER BY time LIMIT 1")
    @Results({
            @Result(property = "parentSonList", column = "parent_son_list"),
            @Result(property = "BroIdList", column = "bro_id_list"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "userContent", column = "user_content"),
            @Result(property = "modelContent", column = "model_content"),
    })
    Messages getRootMessagesByChatId(Integer chatId);

    @Select("SELECT * FROM messages WHERE id = #{id}")
    @Results({
            @Result(property = "parentSonList", column = "parent_son_list"),
            @Result(property = "BroIdList", column = "bro_id_list"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "userContent", column = "user_content"),
            @Result(property = "modelContent", column = "model_content"),
    })
    Messages getMessagesById(Integer id);


    @Select("UPDATE messages SET son_id = #{sonId} WHERE id = #{id}")
    @Results({
            @Result(property = "parentSonList", column = "parent_son_list"),
            @Result(property = "BroIdList", column = "bro_id_list"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "userContent", column = "user_content"),
            @Result(property = "modelContent", column = "model_content"),
    })
    Messages updatePreMessagesSon(Integer id, Integer sonId);

    @Select("SELECT *FROM messages WHERE cid = #{chatID} ORDER BY time DESC LIMIT 1")
    @Results({
            @Result(property = "parentSonList", column = "parent_son_list"),
            @Result(property = "BroIdList", column = "bro_id_list"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "userContent", column = "user_content"),
            @Result(property = "modelContent", column = "model_content"),
    })
    Messages getLastMessagesByChatId(Integer chatId);

    @Insert("Insert into messages (cid,parent_id,parent_son_list,Bro_id_list,user_content,model_content) values (#{chatId},-1,null,null,null,null)")
    void createRootMessages(Integer chatId);

    @Insert("Insert into messages (parent_son_list,Bro_id_list) values (#{rootId},#{rootId})")
    void initRootMessages(Integer rootId);

    @Select("SELECT * from Messages where parent_id = #{parentId}")
    @Results({
            @Result(property = "parentSonList", column = "parent_son_list"),
            @Result(property = "BroIdList", column = "bro_id_list"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "userContent", column = "user_content"),
            @Result(property = "modelContent", column = "model_content"),
    })
    List<Messages> getBroMessageByParentId(Integer parentId);

    @Update("UPDATE messages SET parent_son_list = #{parentSonList} where id = #{id}")
    void updateParentSonListById(String parentSonList, Integer id);

    @Update("UPDATE messages SET bro_id_list = #{broIdList} where id = #{id}")
    void updateBroIdListById(String broIdList, Integer id);
    @Update("UPDATE chat SET last_messages_id = #{lastMessagesId} where id = #{chatId}")
    void updateChatLastMessageId(Integer lastMessagesId, Integer chatId);
    @Select("Select * from chat where id = #{id}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "lastMessagesId", column = "last_messages_id"),
            @Result(property = "chatName", column = "chat_name"),
    })
    Chat getChatByChatId(Integer id);

    @Select("SELECT * from transcript where index_id = #{indexId}")
    @Results({
            @Result(property = "indexId", column = "index_id"),
            @Result(property = "parentSonList", column = "parent_son_list"),
    })
    Transcript getTranscriptByIndexId(Integer indexId);

    @Insert("Insert into transcript (cid,index_id,parent_son_list) values (#{chatId},#{preId},#{parentSonList})")
    void initTranscript(Integer preId, Integer chatId, String parentSonList);

    @Update("UPDATE transcript SET parent_son_list = #{parentSonList} where index_id = #{preId}")
    void updateTranscriptByIndexId(Integer preId, String parentSonList);

    @Delete("DELETE from messages where cid = #{ChatId}")
    void deleteChatMessage(Integer ChatId);

    @Delete("DELETE from transcript where cid = #{ChatId}")
    void deleteChatTranscript(Integer chatId);

    @Delete("DELETE from chat where id = #{ChatId}")
    void deleteChat(Integer ChatId);
}
